package demo02;

import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        FoodManager  fm=new FoodManager();//管理类只能实例化一次
        while (true){
            System.out.println("1）添加新食品");
            System.out.println("2）根据食品名称，查询食品信息");
            System.out.println("3）查询所有商品信息 ");
            System.out.println("4）根据食品名称，修改食品信息");
            System.out.println("5）计算所有食品总价");
            System.out.println("6）统计所有食品中最贵的商品信息");
            System.out.println("7）按照食品的价格升序排序输出这些食品的信息");
            System.out.println("8）退出");
            int choose = input.nextInt();
            switch (choose){
                case 1:
                    addFood(input, fm);
                    break;
                case 2:
                    findByName(input, fm);
                    break;
                case 3:
                    printFood(fm.findAll());
                    break;
                case 4:
                    updateFood(input, fm);
                    break;
                case 5:
                    countPrice(fm);
                    break;
                case 6:
                   printFood(fm.biggestPriceFoodL());
                    break;
                case 7:
                    printFood(fm.sortByPrice());
                    break;
                case 8:
                    System.out.println("谢谢，再见");
                    return;
                default:
                    System.out.println("输入有误，请重新输入！");
            }
        }
    }

    private static void updateFood(Scanner input, FoodManager fm) {
        System.out.println("请输入商品名");
        String name= input.next();
        if (fm.updateByName(name)){
            System.out.println("请修改商品价格");
            double price= input.nextDouble();
            System.out.println("请修改商品的库存");
            int inventory= input.nextInt();
           if (fm.updateByName(name,price,inventory)){
               System.out.println("修改成功");
           }else{
               System.out.println("修改失败");
           }
        }else{
            System.out.println("商品不存在");
        }
    }

    private static void countPrice(FoodManager fm) {
        double price= fm.countPrice();
        System.out.println("所有食品总价为："+price+"元");
    }

    private static void findByName(Scanner input, FoodManager fm) {
        System.out.println("请输入食品名");
        String name= input.next();
        printFood(fm.findByName(name));
    }

    private static void addFood(Scanner input, FoodManager fm) {
        System.out.println("请输入产品名");
        String name= input.next();
        System.out.println("请输入价格");
        double price= input.nextDouble();
        System.out.println("请输入数量");
        int inventory= input.nextInt();
        Food food=new Food(name,price,inventory);
        boolean add= fm.add(food);
        System.out.println(add ? "添加成功!":"添加失败!");
    }

    private static void printFood(Food[] foods) {
        System.out.println("名称\t\t|价格\t\t|库存");
        for (int i = 0; i < foods.length; i++) {
            Food f = foods[i];
            String str = String.format("%s\t\t|%.2f\t\t|%d",f.getName(),f.getPrice(),f.getInventory());
            System.out.println(str);
            //System.out.println(s.getStuId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getGender() + "\t" + s.getPhone());
        }
    }
}
